package com.yiwenup.leetcode.top_interview_150;

import java.util.HashSet;
import java.util.Set;

/**
 * 文件描述
 *
 * @Project struct-algorithm
 * @Package com.yiwenup.leetcode.top_interview_150
 * @Author yiwenup
 * @Date 2024-12-20 22:05:51
 * @Description https://leetcode.cn/problems/happy-number/?envType=study-plan-v2&envId=top-interview-150
 */
public class No0202 {
    Set<Integer> set = new HashSet<>();

    public boolean isHappy(int n) {
        if (set.contains(n)) {
            return false;
        }
        if (n == 1) {
            return true;
        }

        set.add(n);
        int sum = 0;
        while (n >= 10) {
            int p = n % 10;
            sum += p * p;
            n = (n - p) / 10;
        }
        sum += n * n;

        return isHappy(sum);
    }

    public static void main(String[] args) {
        System.out.println(new No0202().isHappy(3));
    }
}
